package cn.wolfcode.business.config;

import cn.wolfcode.business.domain.Appointment;
import cn.wolfcode.business.mapper.AppointmentMapper;
import cn.wolfcode.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 定时器，定时查询超过预约时间的数据
 */
@Component
public class MyTask {
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Scheduled(cron = "0/5 * * * * ?")
    public void executeTask() {
        LocalDateTime now = LocalDateTime.now();
        // 从数据库查询出数据
        List<Appointment> appointmentList = appointmentMapper.selectListByTime(now.minusHours(3));
        // 遍历数据，处理预约时间超过三小时的记录
        for (Appointment appointment : appointmentList) {
            // 处理预约时间超过三小时的记录
            handleExpiredAppointment(appointment);
        }
    }

    private void handleExpiredAppointment(Appointment appointment) {
        // 处理预约时间超过三小时的逻辑
        // 把超过三小时的预约状态设置为过期
        appointment.setStatus(Appointment.STATUS_OVERTIME);
        appointmentMapper.updateAppointment(appointment);
    }
}